/**
 * @(#)AdController.java 2017年4月10日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.dao.IBrushApkRecordDao;
import org.jsbd.boss.dao.IBrushConfigDao;
import org.jsbd.boss.domian.channel.BrushApkRecord;
import org.jsbd.boss.domian.channel.BrushConfig;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO 请加入本类的说明
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/brushApkRecord")
public class BrushApkRecordController {
    public static final Logger LOGGER = LoggerFactory.getLogger(BrushApkRecordController.class);

    @Resource
    private IBrushApkRecordDao recordDao;

    @Resource
    private IBrushConfigDao brushConfigDao;


    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list() {
        return "/brushApkRecord/list";
    }

    @RequestMapping(value = "listdata")
    @ResponseBody
    public Map<String, Object> list(BrushApkRecord record) {
        try {
            Map<String, Object> map = new HashMap<>(4);

            BrushConfig brushConfig = new BrushConfig();
            String brushName = record.getBrushName();
            if (StringUtils.isNotBlank(brushName)) {
                String[] keys = brushName.trim().split(" ");
                List<String> keywords = new ArrayList<>();
                for (String keyword : keys) {
                    keywords.add(keyword);
                }
                brushConfig.setKeywords(keywords);
                brushConfig.setRows(1000);
                List<BrushConfig> brushConfigs = brushConfigDao.selectList(brushConfig);
                if(brushConfigs != null && !brushConfigs.isEmpty()){
                    keywords = new ArrayList<>(4);
                    for(BrushConfig brushConfig1 : brushConfigs){
                        keywords.add(brushConfig1.getId().toString());
                    }
                    record.setKeywords(keywords);
                }else{
                    map.put("total", 0);
                    map.put("rows", new ArrayList<BrushApkRecord>());
                    return map;
                }

            }else{
                record.setBrushName(null);
            }
            String startDay = record.getStartDay();
            String endDay = record.getEndDay();
            if (StringUtils.isNotBlank(startDay)) {
                startDay = startDay.replace("-", "").substring(0, 8);
            } else {
                startDay = DateUtil.getCurrentDateStr();
            }
            if (StringUtils.isNotBlank(endDay)) {
                endDay = endDay.replace("-", "").substring(0, 8);
            } else {
                endDay = DateUtil.getCurrentDateStr();
            }

            record.setStartDay(startDay);
            record.setEndDay(endDay);

            List<BrushApkRecord> brushApkRecords = recordDao.selectList(record);
            for (BrushApkRecord temp : brushApkRecords) {
                String taskNum = temp.getDayLimit() + "|" + (temp.getExecTotal() == null ? 0 : temp.getExecTotal()) + "|" + (temp.getRealExecTotal() == null ? 0 : temp.getRealExecTotal());
                temp.setTaskNum(taskNum);
            }

            map.put("total", recordDao.selectCount(record));
            map.put("rows", brushApkRecords);
            return map;
        } catch (Exception e) {
            LOGGER.error("查询出错", e);
        }
        return null;
    }


    @RequestMapping(value = "apkList")
    public String apkList(BrushApkRecord record, Model model) {
        try {
            Map<String, Object> map = new HashMap<>(4);
            String startDay = record.getStartDay();
            String endDay = record.getEndDay();
            if (StringUtils.isNotBlank(startDay)) {
                startDay = startDay.replace("-", "").substring(0, 8);
            } else {
                startDay = DateUtil.getCurrentDateStr();
            }
            if (StringUtils.isNotBlank(endDay)) {
                endDay = endDay.replace("-", "").substring(0, 8);
            } else {
                endDay = DateUtil.getCurrentDateStr();
            }
            String execDay = record.getExecDay();
            if (StringUtils.isNotBlank(execDay)) {
                startDay = null;
                endDay = null;
            }
            String brushName = record.getBrushName();
            if (StringUtils.isNotBlank(brushName)) {
                String[] keys = brushName.split(" ");
                List<String> keywords = new ArrayList<>();
                for (String keyword : keys) {
                    keywords.add(keyword);
                }
                record.setKeywords(keywords);
            }
            record.setStartDay(startDay);
            record.setEndDay(endDay);
            List<BrushApkRecord> brushApkRecords = recordDao.selectApkList(record);
            for(BrushApkRecord record1 : brushApkRecords){
                if(record1.getRealExecTotal() == null){
                    record1.setRealExecTotal(0);
                }
            }
            model.addAttribute("list", brushApkRecords);
            return "/brushApkRecord/apkList";
        } catch (Exception e) {
            LOGGER.error("查询出错", e);
        }
        return null;
    }


}
